package com.ywk.base.toolClass.scriptEngine.google;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.AviatorEvaluatorInstance;
import com.googlecode.aviator.Expression;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorString;
import com.ywk.base.toolClass.scriptEngine.google.factory.AviatorEvaluatorFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zbh
 * @Date 2025/6/10
 */
public class AviatorEvaluatorTest {
    public static void main(String[] args) {
        f1();
    }

    /**
     * 简单数学运算
     */
    public static void f1(){
        AviatorEvaluatorInstance instance = AviatorEvaluatorFactory.getInstance("Math");
        // 编译表达
//        Expression exp = instance.compile("1 + 2 + 3 * 4");//直接运算
//        Expression exp = instance.compile("a + b");//变量替换
        Expression exp = instance.compile("a + b == 25", false);//


        Map<String, Object> params = new HashMap<>();
        params.put("a", 10);
        params.put("b", 20);

//        Long result = (Long) exp.execute(params); // 输出 30
        boolean result = (Boolean) exp.execute(params);
        System.out.println(result);
    }

    /**
     * 字符串处理
     */
    public static void f2(){
        AviatorEvaluatorInstance instance = AviatorEvaluatorFactory.getInstance("String");
        Expression exp = instance.compile("'Hello, ' + name");
        Map<String, Object> params = new HashMap<>();
        params.put("name", "Aviator");
        String result = (String) exp.execute(params); // 输出 "Hello, Aviator"
        System.out.println(result);
    }

}
